﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace DataLayer
{
    public class NodeContent
    {
        private Node parent;

        public Node Parent
        {
            get { return parent; }
            set { parent = value; }
        }
        private float width;

        public float Width
        {
            get { return width; }
            set { width = value; }
        }
        private float height;

        public float Height
        {
            get { return height; }
            set { height = value; }
        }
        private float top;

        public float Top
        {
            get { return top; }
            set { top = value; }
        }
        private string type;

        private float left;

        public float Left
        {
            get { return left; }
            set { left = value; }
        }
        public string Type
        {
            get { return type; }
            set { type = value; }
        }
        public static List<NodeContent> LoadContentFromXmlNode(XmlNode _parent,Node parent, string _relativePath)
        {
            List<NodeContent> rs = new List<NodeContent>();
            int totalcontents = _parent.ChildNodes.Count;
            for (int i = 0; i < totalcontents; i++)
            {

                XmlNode contentnode = _parent.ChildNodes[i];
                string type = contentnode.Attributes["type"].Value;
                string value = contentnode.InnerText;
                
                NodeContent content;
                
                if (type == "image")
                {
                    if (System.IO.File.Exists(_relativePath + value))
                    {
                        content = new ImageContent(_relativePath + value);
                        content.type = "image";
                        content.parent = parent;
                        rs.Add(content);

                    }
                    content = null;
                }
                else
                {
                    content = new TextContent();
                    content.type = "text";
                    content.parent = parent;
                    int paraCount = contentnode.ChildNodes.Count;
                    for (int p = 0; p < paraCount; p++)
                    {
                        XmlNode paraNode = contentnode.ChildNodes[p];
                        Paragraph para = new Paragraph(contentnode.ChildNodes[p].InnerText);
                        para.Color = int.Parse(paraNode.Attributes["color"].Value);
                        para.IndentLevel = int.Parse(paraNode.Attributes["indentLevel"].Value);
                        para.BulletType = int.Parse(paraNode.Attributes["bulletType"].Value);
                        para.BulletValue = int.Parse(paraNode.Attributes["bulletValue"].Value);
                        para.FontSize = float.Parse(paraNode.Attributes["fontSize"].Value);
                        para.FontName = paraNode.Attributes["fontName"].Value;
                        (content as TextContent).AddParagraph(para);
                    }
                    //((TextContent)content).FontName = contentnode.Attributes["fontName"].Value;
                    //((TextContent)content).FontSize = float.Parse(contentnode.Attributes["fontSize"].Value);

                    rs.Add(content);
                }
                if (content != null)
                {
                    content.Left = float.Parse(contentnode.Attributes["left"].Value);
                    content.Top = float.Parse(contentnode.Attributes["top"].Value);
                    content.Width = float.Parse(contentnode.Attributes["width"].Value);
                    content.Height = float.Parse(contentnode.Attributes["height"].Value);
                }
            }
            return rs;
        }
    }
}
